/*
 * Copyright 2014 Alex Curran
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.espian.showcaseview.sample;

import com.github.amlcurran.showcaseview.LocalHiLog;
import com.github.amlcurran.showcaseview.OnShowcaseEventListener;
import com.github.amlcurran.showcaseview.ShowcaseView;
import com.github.amlcurran.showcaseview.SimpleShowcaseEventListener;
import com.github.amlcurran.showcaseview.targets.ViewTarget;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorGroup;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Button;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.multimodalinput.event.TouchEvent;

/**
 * 显示点击坐标
 */
public class EventsAbility extends Ability {
    private ShowcaseView mShowcaseView;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        ComponentContainer componentContainer = (ComponentContainer) LayoutScatter.getInstance(this)
                .parse(ResourceTable.Layout_ability_events, null, false);
        setUIContent(componentContainer);
        Text eventLog = (Text) findComponentById(ResourceTable.Id_events_log);
        Button customButton = (Button) LayoutScatter.getInstance(this)
                .parse(ResourceTable.Layout_view_custom_button, null, false);
        MultiEventListener multiEventListener = new MultiEventListener(new LogToTextListener(eventLog),
                new ShakeButtonListener(customButton));
        mShowcaseView = new ShowcaseView.Builder(this, (ComponentContainer) componentContainer.getComponentParent())
                .withMaterialShowcase()
                .setStyle(ResourceTable.Pattern_CustomShowcaseTheme3)
                .setTarget(new ViewTarget(ResourceTable.Id_imageView3, this))
                .setContentTitle("Events")
                .setContentText("Listening to ShowcaseView events is easy!")
                .setShowcaseEventListener(multiEventListener)
                .replaceEndButton(customButton)
                .build();
    }

    private static class LogToTextListener implements OnShowcaseEventListener {
        private final Text eventLog;
        private final StringBuilder stringBuilder;

        LogToTextListener(Text eventLog) {
            this.eventLog = eventLog;
            this.stringBuilder = new StringBuilder();
        }

        @Override
        public void onShowcaseViewHide(ShowcaseView showcaseView) {
            append("Showcase hiding");
        }

        private void append(String text) {
            stringBuilder.append("\n").append(text);
            eventLog.setText(stringBuilder.toString());
        }

        @Override
        public void onShowcaseViewDidHide(ShowcaseView showcaseView) {
            append("Showcase hidden");
        }

        @Override
        public void onShowcaseViewShow(ShowcaseView showcaseView) {
            append("Showcase shown");
        }

        @Override
        public void onShowcaseViewTouchBlocked(TouchEvent motionEvent) {
            if (motionEvent.getAction() == TouchEvent.PRIMARY_POINT_UP) {
                append("Touch blocked: x: " + motionEvent.getPointerPosition(0).getX()
                        + " y: " + motionEvent.getPointerPosition(0).getY());
            }
        }
    }

    private class ShakeButtonListener extends SimpleShowcaseEventListener {
        private final Button button;
        private boolean isEnd = true;
        private AnimatorProperty animatorProperty;
        private AnimatorProperty animatorProperty2;

        ShakeButtonListener(Button button) {
            this.button = button;
        }

        @Override
        public void onShowcaseViewTouchBlocked(TouchEvent motionEvent) {
            float translation = 0;
            try {
                translation = getResourceManager().getElement(ResourceTable.Float_touch_button_wobble).getFloat();
            } catch (Exception e) {
                LocalHiLog.warn("ShowcaseView", "button translation not found");
            }
            if (isEnd && motionEvent.getAction() == TouchEvent.PRIMARY_POINT_UP) {
                if (animatorProperty == null) {
                    animatorProperty = button.createAnimatorProperty();
                    animatorProperty.setCurveType(Animator.CurveType.ANTICIPATE_OVERSHOOT);
                    animatorProperty.setLoopedCount(1);
                    animatorProperty.setDuration(70);
                    animatorProperty.moveByX(translation);
                }
                if (animatorProperty2 == null) {
                    animatorProperty2 = button.createAnimatorProperty();
                    animatorProperty2.setCurveType(Animator.CurveType.BOUNCE);
                    animatorProperty2.setLoopedCount(1);
                    animatorProperty2.setDuration(70);
                    animatorProperty2.moveByX(-translation);
                }
                AnimatorGroup animatorGroup = new AnimatorGroup();
                animatorGroup.runSerially(animatorProperty, animatorProperty2);
                animatorGroup.setStateChangedListener(new Animator.StateChangedListener() {
                    @Override
                    public void onStart(Animator animator) {
                        isEnd = false;
                    }

                    @Override
                    public void onStop(Animator animator) {
                        isEnd = true;
                    }

                    @Override
                    public void onCancel(Animator animator) {
                    }

                    @Override
                    public void onEnd(Animator animator) {
                    }

                    @Override
                    public void onPause(Animator animator) {
                    }

                    @Override
                    public void onResume(Animator animator) {
                    }
                });
                animatorGroup.start();
            }
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        mShowcaseView.destory();
    }
}
